package com.stylefeng.guns.modular.bus.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.Config;
import com.stylefeng.guns.core.util.IdGenerator;
import com.stylefeng.guns.core.util.KaptchaUtil;
import com.stylefeng.guns.core.util.OSSUtil;
import com.stylefeng.guns.modular.system.model.Dept;
import com.stylefeng.guns.modular.system.service.IDeptService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.Banner;
import com.stylefeng.guns.modular.bus.service.IBannerService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * banner控制器
 *
 * @author fengshuonan
 * @Date 2018-04-18 16:16:18
 */
@Controller
@RequestMapping("/banner")
public class BannerController extends BaseController {

    private String PREFIX = "/bus/banner/";

    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IDeptService deptService;
    /**
     * 跳转到banner首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "banner.html";
    }

    /**
     * 跳转到添加banner
     */
    @RequestMapping("/banner_add")
    public String bannerAdd() {
        return PREFIX + "banner_add.html";
    }

    /**
     * 跳转到修改banner
     */
    @RequestMapping("/banner_update/{bannerId}")
    public String bannerUpdate(@PathVariable String bannerId, Model model) {
        Banner banner = bannerService.getBannerInfo(bannerId);
        banner.setBannerImgFilePath(OSSUtil.getFileUrl(banner.getBannerImgFilename(),99999));
        model.addAttribute("item",banner);
        LogObjectHolder.me().set(banner);
        return PREFIX + "banner_edit.html";
    }

    /**
     * 获取banner列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String comId,@RequestParam(required = false) String prdId) {
        Map<String,String > map=new HashMap();
        map.put("comId",comId);
        map.put("prdId",prdId);
        Dept dept=deptService.selectById(ShiroKit.getUser().getDeptId());
        Integer comPid=dept.getPid();
        KaptchaUtil.comAuth(map,comPid,String.valueOf(ShiroKit.getUser().getDeptId()));
       List<Banner> bannerList= bannerService.getBannerList4ComId(map);
        for (int i = 0; i < bannerList.size(); i++) {
            Banner banner=bannerList.get(i);
            banner.setBannerImgFilePath(OSSUtil.getFileUrl(banner.getBannerImgFilename(),Config.expires));
        }
        return bannerList;
    }
    @RequestMapping(value = "/list4Front")
    @ResponseBody
    public Object list4Front(@RequestParam("prdNo") String prdNo) {
        List<Banner> bannerList= bannerService.getBannerList4PrdNo(prdNo);
        for (int i = 0; i < bannerList.size(); i++) {
            Banner banner=bannerList.get(i);
            banner.setBannerImgFilePath(OSSUtil.getFileUrl(banner.getBannerImgFilename(), Config.expires));
        }
        return bannerList;
    }
    /**
     * 新增banner
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Banner banner) {
        banner.setId(IdGenerator.getId());
        banner.setCreatetime(new Date());
        banner.setDelFlag(0);
        banner.setStatus(0);
        banner.setMaintainer(String.valueOf(ShiroKit.getUser().getId()));
        bannerService.insert(banner);
        return SUCCESS_TIP;
    }

    /**
     * 删除banner
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String bannerId) {
        /*Banner banner=bannerService.selectById(bannerId);
        banner.setDelFlag(1);
        bannerService.updateById(banner);*/
        bannerService.deleteById(bannerId);
        return SUCCESS_TIP;
    }

    /**
     * 修改banner
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Banner banner) {
        bannerService.updateById(banner);
        return SUCCESS_TIP;
    }

    /**
     * banner详情
     */
    @RequestMapping(value = "/detail/{bannerId}")
    @ResponseBody
    public Object detail(@PathVariable("bannerId") String bannerId) {
        Banner banner=bannerService.selectById(bannerId);
        banner.setBannerImgFilePath(OSSUtil.getFileUrl(banner.getBannerImgFilename(),99999));
        return banner;
    }
}
